<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Debug;
use app\models\{Config};
use think\Db;
use think\facade\Log;
use app\facade\Redis;


class PtDatas extends Command
{
    protected function configure()
    {
        $this->setName('PtDatas')->setDescription('平台数据');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        ini_set('max_execution_time', 0);
        $ptdatas = (new Config())->where('name', 'ptdatas')->value('value');
        $ptdatas = json_decode($ptdatas, true);
        if (!empty($ptdatas['total'])) {
            //判断是否可以再刷
            $name = 'ptdatas_total';
            $totalredis = Redis::get($name);
            if (!$totalredis) {
                $xztotal = rand($ptdatas['total']['min'] * 100, $ptdatas['total']['max'] * 100) / 100;
                $ptdatas['total']['money'] = floor(($ptdatas['total']['money'] + $xztotal) * 100) / 100;
                $ptdatas['total']['money'] = $ptdatas['total']['money'] > 0 ? $ptdatas['total']['money'] : 0;
                Redis::setex($name, (int)$ptdatas['total']['time'], 1);
            }
        }
        if (!empty($ptdatas['user'])) {
            //判断是否可以再刷
            $name = 'ptdatas_user';
            $totalredis = Redis::get($name);
            if (!$totalredis) {
                $xztotal = rand($ptdatas['user']['min'], $ptdatas['user']['max']);
                $ptdatas['user']['money'] = (int)($ptdatas['user']['money'] + $xztotal);
                $ptdatas['user']['money'] = $ptdatas['user']['money'] > 0 ? $ptdatas['user']['money'] : 0;
                Redis::setex($name, (int)$ptdatas['user']['time'], 1);
            }
        }
        if (!empty($ptdatas['huser'])) {
            //判断是否可以再刷
            $name = 'ptdatas_huser';
            $totalredis = Redis::get($name);
            if (!$totalredis) {
                $xztotal = rand($ptdatas['huser']['min'], $ptdatas['huser']['max']);
                $ptdatas['huser']['money'] = (int)($ptdatas['huser']['money'] + $xztotal);
                $ptdatas['huser']['money'] = $ptdatas['huser']['money'] > 0 ? $ptdatas['huser']['money'] : 0;
                if ($ptdatas['huser']['money'] > $ptdatas['user']['money']) {
                    $ptdatas['huser']['money'] = (int)($ptdatas['user']['money'] * 0.6);
                }
                Redis::setex($name, (int)$ptdatas['user']['time'], 1);
            }
        }
        if (!empty($ptdatas['fwnumber'])) {
            //判断是否可以再刷
            $name = 'ptdatas_fwnumber';
            $totalredis = Redis::get($name);
            if (!$totalredis) {
                $xztotal = rand($ptdatas['fwnumber']['min'], $ptdatas['fwnumber']['max']);
                $ptdatas['fwnumber']['money'] = (int)($ptdatas['fwnumber']['money'] + $xztotal);
                $ptdatas['fwnumber']['money'] = $ptdatas['fwnumber']['money'] > 0 ? $ptdatas['fwnumber']['money'] : 0;
                Redis::setex($name, (int)$ptdatas['fwnumber']['time'], 1);
            }
        }
        if (!empty($ptdatas['xznumber'])) {
            //判断是否可以再刷
            $name = 'ptdatas_xznumber';
            $totalredis = Redis::get($name);
            if (!$totalredis) {
                $xztotal = rand($ptdatas['xznumber']['min'], $ptdatas['xznumber']['max']);
                $ptdatas['xznumber']['money'] = (int)($ptdatas['xznumber']['money'] + $xztotal);
                $ptdatas['xznumber']['money'] = $ptdatas['xznumber']['money'] > 0 ? $ptdatas['xznumber']['money'] : 0;
                Redis::setex($name, (int)$ptdatas['xznumber']['time'], 1);
            }
        }
        if (!empty($ptdatas['fnnumber'])) {
            //判断是否可以再刷
            $name = 'ptdatas_fnnumber';
            $totalredis = Redis::get($name);
            if (!$totalredis) {
                $xztotal = rand($ptdatas['fnnumber']['min'], $ptdatas['fnnumber']['max']);
                $ptdatas['fnnumber']['money'] = (int)($ptdatas['fnnumber']['money'] + $xztotal);
                $ptdatas['fnnumber']['money'] = $ptdatas['fnnumber']['money'] > 0 ? $ptdatas['fnnumber']['money'] : 0;
                Redis::setex($name, (int)$ptdatas['fnnumber']['time'], 1);
            }
        }
        if (!empty($ptdatas['fallnnumber3'])) {
            //判断是否可以再刷
            $name = 'ptdatas_fallnnumber3';
            $totalredis = Redis::get($name);
            if (!$totalredis) {
                $xztotal = rand($ptdatas['fallnnumber3']['min'], $ptdatas['fallnnumber3']['max']);
                $ptdatas['fallnnumber3']['money'] = (int)($ptdatas['fallnnumber3']['money'] + $xztotal);
                $ptdatas['fallnnumber3']['money'] = $ptdatas['fallnnumber3']['money'] > 0 ? $ptdatas['fallnnumber3']['money'] : 0;
                Redis::setex($name, (int)$ptdatas['fallnnumber3']['time'], 1);
            }
        }
        if (!empty($ptdatas['fallnnumber0'])) {
            //判断是否可以再刷
            $name = 'ptdatas_fallnnumber0';
            $totalredis = Redis::get($name);
            if (!$totalredis) {
                $xztotal = rand($ptdatas['fallnnumber0']['min'], $ptdatas['fallnnumber0']['max']);
                $ptdatas['fallnnumber0']['money'] = (int)($ptdatas['fallnnumber0']['money'] + $xztotal);
                $ptdatas['fallnnumber0']['money'] = $ptdatas['fallnnumber0']['money'] > 0 ? $ptdatas['fallnnumber0']['money'] : 0;
                Redis::setex($name, (int)$ptdatas['fallnnumber0']['time'], 1);
            }
        }
        if (!empty($ptdatas['fallnnumber1'])) {
            //判断是否可以再刷
            $name = 'ptdatas_fallnnumber1';
            $totalredis = Redis::get($name);
            if (!$totalredis) {
                $xztotal = rand($ptdatas['fallnnumber1']['min'], $ptdatas['fallnnumber1']['max']);
                $ptdatas['fallnnumber1']['money'] = (int)($ptdatas['fallnnumber1']['money'] + $xztotal);
                $ptdatas['fallnnumber1']['money'] = $ptdatas['fallnnumber1']['money'] > 0 ? $ptdatas['fallnnumber1']['money'] : 0;
                Redis::setex($name, (int)$ptdatas['fallnnumber1']['time'], 1);
            }
        }
        if (!empty($ptdatas['fallnnumber2'])) {
            //判断是否可以再刷
            $name = 'ptdatas_fallnnumber2';
            $totalredis = Redis::get($name);
            if (!$totalredis) {
                $ptdatas['fallnnumber2']['money'] = (int)($ptdatas['fallnnumber3']['money'] - $ptdatas['fallnnumber1']['money'] - $ptdatas['fallnnumber0']['money']);
                $ptdatas['fallnnumber2']['money'] = $ptdatas['fallnnumber2']['money'] > 0 ? $ptdatas['fallnnumber2']['money'] : 0;
                Redis::setex($name, (int)$ptdatas['fallnnumber2']['time'], 1);
            }
        }
        if (!empty($ptdatas['monethclass'])) {
            //判断是否可以再刷
            foreach ($ptdatas['monethclass'] as &$class) {
                $name = 'ptdatas_fnnumber_' . $class['key'];
                $totalredis = Redis::get($name);
                if (!$totalredis) {
                    $xztotal = rand($class['min'], $class['max']);
                    $class['value'] = (int)($class['value'] + $xztotal);
                    $class['value'] = $class['value'] > 0 ? $class['value'] : 0;
                    Redis::setex($name, (int)$class['times'], 1);
                }
            }
            $ptdatas['monethclass'] = array_values($ptdatas['monethclass']);
        }
        if (!empty($ptdatas['orderuser'])) {
            $ptdatas['orderuser'] = array_values($ptdatas['orderuser']);
            $counts = count($ptdatas['orderuser']);
            //获取名字
            if ($counts > 0) {
                $namekey = rand(0, $counts - 1); //名字下标
                $mobilekey = rand(0, $counts - 1); //电话下标
                $status = rand(2, 4); //状态下标
                if ($status == 2) {
                    $status = '用户已下单';
                }
                if ($status == 3) {
                    $status = '用户已接收服务';
                }
                if ($status == 4) {
                    $status = '用户已确认';
                }
                $orderlist = [$ptdatas['orderuser'][$namekey]['key'], $ptdatas['orderuser'][$mobilekey]['value'], $status];
                Redis::lPush('ptdatas_lists', serialize($orderlist));
            }
        }
        $ptdatas = json_encode($ptdatas, true);
        (new Config())->where('name', 'ptdatas')->update(['value' => $ptdatas]);
        echo '成功';
    }
}
